/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jrill.notification;

import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.jrill.HumanActivity;
import org.jrill.Participant;

/**
 * Notify by Simple Mail Protocol.
 * Must be injected in {@link Workflow} in some fashion (e.g. AspectJ)
 * @author K.Waga <kzwaga@gmail.com>
 */
public class SmtpNotifier implements Notifier {
    /** property key for smtp host */
    public static final String PROP_KEY_SMTPHOST = "mail.smtp.host";
    private static final String MAIL_DEFAULT_ENCODING = "utf-8";


    /**
     * SMTP Server properties
     */
    private String smtpHost;
    /**
     * {@inheritDoc }
     * @param sendTo
     * @param copyTo
     * @param blindCopyTo
     * @param messageTemplate 
     */
    public void send(Participant[] sendTo, Participant[] copyTo, Participant[] blindCopyTo, MessageTemplate messageTemplate) {
        assert null != sendTo && sendTo.length > 0 : "TO participant must be given.";
        try {
            InternetAddress[] to = toInternetAddresses(sendTo);
            Properties props = new Properties();
            props.put(PROP_KEY_SMTPHOST, getSmtpHost());
            Session s = Session.getInstance(props);
            MimeMessage message = new MimeMessage(s);
            message.setRecipients(RecipientType.TO, to);
            if (copyTo != null && copyTo.length > 0) {
                message.setRecipients(RecipientType.CC, toInternetAddresses(copyTo));
            }
            if (blindCopyTo != null && blindCopyTo.length > 0) {
                message.setRecipients(RecipientType.BCC, toInternetAddresses(blindCopyTo));
            }
            message.setSubject(messageTemplate.evaluateSubject(), MAIL_DEFAULT_ENCODING);
            StringBuilder body = new StringBuilder();
            body.append(messageTemplate.evaluateBody());
            String link = messageTemplate.evaluateLinkUrl();
            if (link != null) body.append('\n').append(link);
            message.setText(body.toString(), MAIL_DEFAULT_ENCODING);
            Transport.send(message);
        } catch (MessagingException ex) {
            Logger.getLogger(SmtpNotifier.class.getName()).log(Level.SEVERE, "Something is wrong with mail transfer!", ex);
        }
    }

    
    private InternetAddress[] toInternetAddresses(Participant[] participant) {
        InternetAddress[] addr = new InternetAddress[participant.length];
        try {
            for (int i = 0; i < participant.length; i++) {
                addr[i] = new InternetAddress(participant[i].getAddress());
            }
            return addr;
        } catch (AddressException ex) {
            Logger.getLogger(SmtpNotifier.class.getName()).log(Level.SEVERE, null, ex);
            throw new RuntimeException(ex);
        }
    }

    /**
     * @return the smtpHost
     */
    public String getSmtpHost() {
        return smtpHost;
    }

    /**
     * @param smtpHost the smtpHost to set
     */
    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

}
